/**
 * @desc 小程序权限获取
 * @param {Object} name 授权名称
 * @return {Promise[Object]}
 */
export default function(name) {
	return new Promise(async (resolve, reject) => {
		// #ifdef H5
		return resolve({ code: 200 });
		// #endif

		// #ifdef MP-TOUTIAO
		name === 'scope.writePhotosAlbum' && (name = 'scope.album')
		// #endif

		const { authSetting } = await uni.getSetting();
		if (authSetting[name]) {
			return resolve({ code: 200 });
		}

		// 进行授权
		try {
			// 授权成功
			await uni.authorize({ scope: name });
			return resolve({ code: 200 });
		} catch {}

		// 重新授权
		uni.showModal({
			title: '提示',
			content: '权限授权失败，重新授权吗？',
			success: (res) => {
				if (res.confirm) {
					uni.openSetting({
						success({ authSetting }) {
							if (authSetting[name]) {
								return resolve({ code: 200 });
							}
							resolve({ code: 210, err: '授权失败' })
						}
					});
				} else {
					resolve({ code: 210, err: '授权失败' })
				}
			}
		})
	})
}